package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.nifa.NifaData;
import com.ucredit.babel.common.model.nifa.NifaQueryRecord;
import com.weicai.caesar.CaesarUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 中互金查询
 *
 * @author rrz
 */

public class NifaQueryParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * DP平台互金
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        NifaQueryRecord obj = (NifaQueryRecord) model;
        NifaData data;
        JSONObject info;
        try {
            data = obj.getData();
            info = JSON.parseObject(data.getData());
        } catch (java.lang.NullPointerException ignored) {
            return true;
        }


        StringBuilder sb = getBaseInfo(obj, successful);
        // 姓名
        sb.append(data.getName()).append(CRT001);
        // 身份证类型
        sb.append(data.getIdtype()).append(CRT001);
        // 身份证号
        sb.append(StringUtils.isBlank(data.getNo()) ? "" : CaesarUtil.isEncrypted(data.getNo()) ? data.getNo() : CaesarUtil.encode(data.getNo())).append(CRT001);
        // 查询原因
        sb.append(data.getReason()).append(CRT001);

        // 账户数
        sb.append(info.getString("loancount") == null ? "" : info.getString("loancount")).append(CRT001);
        // 机构数
        sb.append(info.getString("loanorgcount") == null ? "" : info.getString("loanorgcount")).append(CRT001);
        // 循环类授信金额
        sb.append(info.getString("circularcreditamt") == null ? "" : info.getString("circularcreditamt")).append(CRT001);
        // 非循环类累计借款金额
        sb.append(info.getString("acyclicloadamt") == null ? "" : info.getString("acyclicloadamt")).append(CRT001);
        // 未结清/销户账户数
        sb.append(info.getString("outstandcount") == null ? "" : info.getString("outstandcount")).append(CRT001);
        // 借款余额
        sb.append(info.getString("loanbal") == null ? "" : info.getString("loanbal")).append(CRT001);
        // 当前违约账户数
        sb.append(info.getString("overduecount") == null ? "" : info.getString("overduecount")).append(CRT001);
        // 当前违约金额
        sb.append(info.getString("overdueamt") == null ? "" : info.getString("overdueamt")).append(CRT001);
        // 当前不良账户数
        sb.append(info.getString("overduemorecount") == null ? "" : info.getString("overduemorecount")).append(CRT001);
        // 当前不良金额
        sb.append(info.getString("overduemoreamt") == null ? "" : info.getString("overduemoreamt")).append(CRT001);
        // 网络借贷信息中介机构的借款总余额
        sb.append(info.getString("loanorgbal") == null ? "" : info.getString("loanorgbal")).append(CRT001);
        // 保证人代还账户数
        sb.append(info.getString("insurancecount") == null ? "" : info.getString("insurancecount")).append(CRT001);
        // 保证人代还机构数
        sb.append(info.getString("insuranceorgcount") == null ? "" : info.getString("insuranceorgcount")).append(CRT001);
        // 保证人代还累计代偿金额
        sb.append(info.getString("insuranceamt") == null ? "" : info.getString("insuranceamt")).append(CRT001);
        // 保证人代还余额
        sb.append(info.getString("insurancebal") == null ? "" : info.getString("insurancebal")).append(CRT001);
        // 最近1个月机构查询情况
        sb.append(info.getString("totalorg") == null ? "" : info.getString("totalorg")).append(CRT001);
        // 最近1个月以“贷前审批”原因查询情况
        sb.append(info.getString("queryatotalorg") == null ? "" : info.getString("queryatotalorg")).append(CRT001);
        // 明细信息
        sb.append(info.getString("infoquerybean") == null ? "" : info.getString("infoquerybean")).append(CRT001);
        // 当前担保人代还金额
        sb.append(info.getString("generationamount") == null ? "" : info.getString("generationamount")).append(CRT001);
        // 返回状态
        sb.append(info.getString("msgContent") == null ? "" : info.getString("msgContent")).append(CRT001);
        // 错误码
        sb.append(info.getString("msgCode") == null ? "" : info.getString("msgCode")).append(CRT001);
        StringBuilder txSb = getBaseInfo(obj);
        key.set(getKey());
        val.set((sb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getSubType() {
        return ".main";
    }

    @Override
    public String getTableName() {
        return "kafka_babel_nifa_query_new_inc";
    }

    @Override
    public String getType() {
        return RecordType.NIFA_QUERY.toString();
    }

    @Override
    public String getComment() {
        return "中互金查询";
    }
}
